package miiteen.demo.web.controller;


import javafx.beans.binding.ObjectExpression;
import miiteen.core.configuration.MeetingProperties;
import miiteen.core.result.ResultMessage;
import miiteen.core.result.ResultMessageTool;
import miiteen.customsportal.basic.beans.BasicBrowsingHistory;
import miiteen.customsportal.basic.service.BasicBrowsingHistoryService;
import miiteen.demo.HB2012Service.Service;
import miiteen.demo.HB2012Service.XmlInfo;
import miiteen.demo.utils.CommonUtil;
import miiteen.demo.web.beans.ListPageInfo;
import miiteen.demo.web.beans.WebInfo;
import miiteen.demo.web.service.ListPageInfoService;
import miiteen.demo.web.service.WebInfoService;
import miiteen.sys.base.controller.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import miiteen.common.json.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Controller
@RequestMapping(value = "/demo/web/webinfo")
@CrossOrigin
public class WebInfoController extends BaseAction {

    @Autowired
    private WebInfoService webInfoService;

    @Autowired
    private CommonUtil commonUtil;


    @Autowired
    private ListPageInfoService listPageInfoService;

    @Autowired
    private BasicBrowsingHistoryService basicBrowsingHistoryService;


    @Autowired
    private Service HB2012Service;

    /**
     * 获取门户网全部模块信息
     *
     * @param
     * @param start 起始
     * @param count 数量
     * @param
     * @return
     */
    @RequestMapping("/getWebList")
    @ResponseBody
    public ResultMessage<Object> getWebInfo(@RequestParam String start, @RequestParam String count) {


        Map<String, Object> map = new HashMap<>();
        map.put("noticeList", webInfoService.getNotice(start, count + 1));//通知公告
        map.put("functionList", webInfoService.getFunction(start, count));//职能管理
        map.put("documentList", webInfoService.getDocument(start, count));//兰关发文
        map.put("minuteList", webInfoService.getMinutes(start, count));//会议纪要
        map.put("trendList", webInfoService.getTrends(start, count + 2));//兰关动态
        map.put("reportsList;", webInfoService.getReports(start, count + 2));//工作简报
        map.put("notificationList;", webInfoService.getNotification(start, count));//内部情况通报
        map.put("bulletinList;", webInfoService.getBulletin(start, count));//检查通报
        //   map.put("DBlist;",getDBInfo(start,count));//关级督办
        if (map.size() > 0) {
            return ResultMessageTool.Success("查询成功", map);
        } else {
            return ResultMessageTool.Error("查询失败");
        }

    }


    /**
     * 获取督办信息,包含有督办数量，督办列表，分别对应的key为TotalCount，resourceList
     *
     * @param start
     * @param count
     * @return
     */
    @RequestMapping("/getDBList")
    @ResponseBody
    public ResultMessage<Object> getDBInfo(@RequestParam String start, @RequestParam String count) {
        Map<String, Object> map = new HashMap<>();
        try {
            map = HB2012Service.getDBInfo(start, count);
        } catch (Exception e) {
            return ResultMessageTool.Error("查询出错");
        }

        if (map.get("resourceList") != null) {
            return ResultMessageTool.Success("查询成功", map);
        } else {
            return ResultMessageTool.Error("查询失败");
        }
    }

    /**
     * 获取用户待办任务数量
     *
     * @param userGuid 登录用户的工号
     * @return
     */
    @RequestMapping("/getTaskCount")
    @ResponseBody
    public ResultMessage<Object> getTaskCount(@RequestParam String userGuid) {

        Map<String, Object> map = new HashMap<>();

        try {
            map = HB2012Service.getTaskList(userGuid);
        } catch (Exception e) {
            System.out.println(e);
            return ResultMessageTool.Success();
            // return ResultMessageTool.Error("查询出错");
        }

        if (map.size() > 0) {
            return ResultMessageTool.Success("查询成功", map);
        } else {
            return ResultMessageTool.Error("查询失败");
        }

    }

    /**
     * 查询门户网部分模块信息
     *
     * @param
     * @param start 起始
     * @param count 数量
     * @param
     * @return
     */
    @RequestMapping("/searchInfoList")
    @ResponseBody
        public ResultMessage<Object> searchList(@RequestParam String tableName, @RequestParam String start, @RequestParam String count, @RequestParam String SNCode, @RequestParam String keyWords) {

        /*模块  tableName SNCode
         *通知公告 one TZGG
         *职能管理 one DTLM
         *兰关发文 two LGFW
         *会议纪要 two HYJY
         *兰关动态 three LGDT
         *工作简报 three GZJB
         * 内部情况通报 three NBQKTB
         * 检查通报 three  TBT
         * */

        Map<String, Object> map = new HashMap<>();

        map.put("tableName", tableName);
        int startNum = Integer.parseInt(start);
        map.put("start", startNum > 1 ? 1 + 10 * (startNum - 1) : startNum);
        map.put("count", Integer.parseInt(count));
        map.put("SNCode", SNCode);
        if (!"".equals(keyWords)) {
            map.put("keyWords", keyWords);
        }

        commonUtil.handleLikeSearch(map, "keyWords");
        List<WebInfo> list = webInfoService.findBySNCode(map);
        Map<String, Object> limitMap = new HashMap<>();

        for (WebInfo webInfo : list) {
            limitMap.clear();
            limitMap.put("id", webInfo.getResourceId());
            limitMap.put("ifMod", 0);
            BasicBrowsingHistory byIdNumber = basicBrowsingHistoryService.getByIdNumber(limitMap);
            webInfo.setTodayNumber(byIdNumber == null ? 0 : byIdNumber.getDayNumber());
            webInfo.setTotalNumber(byIdNumber == null ? 0 : byIdNumber.getAllNumber());
        }
        ListPageInfo listPageInfo = listPageInfoService.findTotalCount(map);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("infoList", list);
        resultMap.put("totalCount", listPageInfo.getTotalCount());
        if (resultMap.size() > 0) {
            return ResultMessageTool.Success("查询成功", resultMap);
        } else {
            return ResultMessageTool.Error("查询失败");
        }

    }

    /**
     * 获取督办信息,包含有督办数量，督办列表，分别对应的key为TotalCount，resourceList
     *
     * @param start
     * @param count
     * @return
     */
    @RequestMapping("/searchDBList")
    @ResponseBody
    public ResultMessage<Object> searchDBInfo(@RequestParam String start, @RequestParam String count, @RequestParam String keyWords) {
        Map<String, Object> map = new HashMap<>();
        List<XmlInfo> newList = new ArrayList<>();
        try {
            int startNum = Integer.parseInt(start);
            startNum = 10 * (startNum - 1);
            map = HB2012Service.getDBInfo(startNum + "", count);
            List<XmlInfo> list = (List<XmlInfo>) map.get("resourceList");
            if (!"".equals(keyWords)) {
                map = HB2012Service.getDBInfo("0", "100");
                List<XmlInfo> list2 = (List<XmlInfo>) map.get("resourceList");
                for (XmlInfo xmlInfo : list2) {
                    if (xmlInfo.getTitle().contains(keyWords)) {
                        newList.add(xmlInfo);
                    }
                }
            } else {
                newList = list;
            }
            Map<String, Object> limitMap = new HashMap<>();
            for (XmlInfo xmlInfo:list
                 ) {
                limitMap.clear();
                limitMap.put("id", xmlInfo.getId());
                limitMap.put("ifMod", 0);
                BasicBrowsingHistory byIdNumber = basicBrowsingHistoryService.getByIdNumber(limitMap);
                xmlInfo.setTodayNumber(byIdNumber == null ? 0 : byIdNumber.getDayNumber());
                xmlInfo.setTotalNumber(byIdNumber == null ? 0 : byIdNumber.getAllNumber());
            }

            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("infoList", newList);
            int totalCount = Integer.parseInt(MeetingProperties.getConfigByKey("webInfo.DBInfoTotalCount"));
            if (!"".equals(keyWords)) {
                resultMap.put("totalCount", newList.size());
            } else {
                resultMap.put("totalCount", totalCount);
            }

            if (newList.size() > 0) {
                return ResultMessageTool.Success("查询成功", resultMap);
            } else {
                return ResultMessageTool.Error("查询失败");
            }
        } catch (Exception e) {
            return ResultMessageTool.Error("查询出错");
        }
    }


}
